package cn.humaohua.study.hadoop.cmdrunner.c3;


import cn.humaohua.study.hadoop.cmdrunner.Test;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.io.IOUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import java.io.InputStream;
import java.net.URL;
@Component
public class ReadingDataFromHadoopURL implements Test {

    /**
     * Exception in thread "main" java.lang.IllegalStateException: Failed to execute CommandLineRunner
     * 	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816)
     * 	at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797)
     * 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
     * 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
     * 	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
     * 	at cn.humaohua.study.hadoop.HadoopApplication.main(HadoopApplication.java:19)
     * Caused by: java.net.MalformedURLException: unknown protocol: hdfs
     */
    static {
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
    }

    @Override
    public void test() throws Exception {
        InputStream in = null;
        try {
            in = new URL("hdfs://localhost/user/hmh/input/ncdc/sample.txt").openStream();
// process in
//            FileCopyUtils.copy(in,System.out);
            IOUtils.copyBytes(in, System.out, 4096, false);
        } finally {
            IOUtils.closeStream(in);
        }
    }
}
